'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 玩家BOSS战信息
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var mongoose = require('mongoose');
var bossItemSchema = require('./boss/bossItemSchema');
var constantUtil = require('../util/constantUtil');
var ObjectId = mongoose.Types.ObjectId;

/**
 * 初始化战斗次数
 * @return {[type]} [description]
 */
var _initBattleNum = function () {
    return constantUtil.value('boss_challenge_times');
};

/**
 * 玩家BOSS战信息
 */
var bossSchema = new mongoose.Schema({
    // 角色id
    role_id: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 最大伤害
    max_damage: {
        type: Number,
        default: 0
    },

    // 剩余战斗的次数
    battle_num: {
        type: Number,
        default: _initBattleNum
    },

    // 选中的士兵列表
    select_soldier_list: {
        type: [Number],
        default: []
    },

    // 出战战力
    fight_power: {
        type: Number,
        default: 0
    },
});

bossSchema.pre('save', function (next) {
    this.increment();
    if (!this.isNew && !!this.isModified()) {
        this.pushModify();
    }
    next();
});

/**
 * 设置伤害
 * @param {[type]} damage [description]
 */
bossSchema.methods.setDamage = function (damage) {
    if (this.max_damage >= damage) {
        return;
    }
    this.max_damage = damage;
};

/**
 * 获取最大伤害
 * @return {[type]} [description]
 */
bossSchema.methods.getMaxDamage = function () {
    return this.max_damage;
};

/**
 * 获取已经战斗的时间
 * @return {[type]} [description]
 */
bossSchema.methods.getBattleNum = function () {
    return this.battle_num;
};

/**
 * 扣除战斗的次数
 * @return {[type]} [description]
 */
bossSchema.methods.subBattleNum = function () {
    if (this.battle_num <= 0) {
        return false;
    }

    this.battle_num--;
    return true;
};

/**
 * 获取上次刷新对手的时间
 * @return {[type]} [description]
 */
bossSchema.methods.getLastRefreshTime = function () {
    return this.last_refresh_time;
};

/**
 * 设置BOSS战出战士兵
 */
bossSchema.methods.setSelectSoldierList = function (select_list) {
    this.select_soldier_list = _.uniq(select_list);
};

/**
 * 刷新
 * @return {[type]} [description]
 */
bossSchema.methods.refresh = function () {
    this.battle_num = _initBattleNum();
};

/**
 * 推送
 * @return {[type]} [description]
 */
bossSchema.methods.push = function () {};

/**
 * 推送修改
 * @return {[type]} [description]
 */
bossSchema.methods.pushModify = function () {};

if (!bossSchema.options.toJSON) {
    bossSchema.options.toJSON = {};
}
/* jshint unused:false */
bossSchema.options.toJSON.transform = function (doc, ret) {
    delete ret.role_id;
    delete ret._id;
    delete ret.__v;
};

mongoose.model('Boss', bossSchema);
